//
//  VisitorLoginView.swift
//  CLWeiBo
//
//  Created by 程雷 on 15/6/24.
//  Copyright © 2015年 程雷. All rights reserved.
//

import UIKit
import FFAutoLayout

//访客视图协议 － 协议需要继承自 NSObjectProtocol

protocol VisitorLoginViewDelegate:NSObjectProtocol
{
    func VisitorRegisterButtonClicked()
    func VisitorLoginButtonClicked()
}

class VisitorLoginView: UIView {
    
    // 代理  weak id<VisitorLoginViewDelegate> delegate
    weak var delegate:VisitorLoginViewDelegate?
    /**
    设置视图
    
    :param: iconName 图标名称
    :param: message  文本消息
    :param: isHome   是否是消息首页
    */
    func setupViewInfo(iconName:String,message:String,isHome:Bool)
    {
        messageLabel.text = message
        if isHome
        {
            homeIconView.hidden = false
            iconView.image = UIImage(named: "visitordiscover_feed_image_smallicon")
        }
        else
        {
            homeIconView.hidden = true
            iconView.image = UIImage(named: iconName)
            //将层次视图设置后
            sendSubviewToBack(maskIconView)
        }
    }
    
    /**
    启动动画
    */
    func startAnimation(){
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        anim.toValue = 2*M_PI
        anim.duration = 20
        anim.repeatCount = MAXFLOAT
        
        iconView.layer.addAnimation(anim, forKey: nil)
    }
    //注册方法
    func register(){
        delegate?.VisitorRegisterButtonClicked()
    }
    //登录方法
    func login(){
        delegate?.VisitorLoginButtonClicked()
    }
    
    // 视图的默认构造方法
    // 创建控件，并且指定布局，用 xib & sb 开发不会调用
    // 这个方法 只会在纯代码手写的时候会调用，
    override init (frame:CGRect)
    {
        super.init(frame: frame)
        //加载创建控件的放啊
        setupUI()
        
    }
    // 用 xib & sb 开发的时候会调用这个方法
    required init(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    /**
    添加控件
    */
    private func setupUI(){
        
        // 添加空间  －－－能够看清各个控件间的层次关系
        addSubview(iconView)
        addSubview(maskIconView)
        addSubview(homeIconView)
        addSubview(messageLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        
        // 2.自动布局
        // 2.1 设置文字图片，那个一圈文字的图片，水平居中垂直居中
        iconView.ff_AlignInner(ff_AlignType.CenterCenter, referView: self, size: nil, offset: CGPoint(x: 0, y: -40))
        
        // 2.2设置 文字图片中间的小房子
        homeIconView.ff_AlignInner(ff_AlignType.CenterCenter, referView: iconView, size: nil)
        // 2.3 设置文字
        messageLabel.ff_AlignVertical(ff_AlignType.BottomCenter, referView: iconView, size: CGSize(width: 240, height: 40), offset: CGPoint(x:0,y:20))
        
        // 2.4 注册按钮
        registerButton.ff_AlignVertical(ff_AlignType.BottomLeft, referView: messageLabel, size: CGSize(width: 100, height: 40), offset: CGPoint(x: 0, y: 20))
        
        // 2.5 登录按钮
        loginButton.ff_AlignVertical(ff_AlignType.BottomRight, referView: messageLabel, size: CGSize(width: 100, height: 40), offset: CGPoint(x: 0, y: 20))
        
        //2.6 遮罩
        maskIconView.ff_Fill(self, insets: UIEdgeInsets(top: 0, left: 0, bottom: 160, right: 0))
        maskIconView.translatesAutoresizingMaskIntoConstraints = false
        // 背景颜色
        backgroundColor = UIColor(white: 237.0/255.0, alpha: 1.0)
        
    }
    
    /**
    *  懒加载控件
    */
    // 图标
    lazy var iconView: UIImageView = {
        let image = UIImage(named: "visitordiscover_feed_image_smallicon")
        return UIImageView(image: image)
    }()
    // 遮罩图片， 不要写 maskView
    lazy var maskIconView: UIImageView = {
        let image = UIImage(named: "visitordiscover_feed_mask_smallicon")
        return UIImageView(image: image)
    }()
    // 房子
    lazy var homeIconView: UIImageView = {
        let image = UIImage(named: "visitordiscover_feed_image_house")
        return UIImageView(image: image)
    }()
    
    // 文字标签
    lazy var messageLabel: UILabel = {
        // 一旦使用了自动布局 ，frame就失效了 ，在添加控件布局时有效
        let label = UILabel(frame: CGRectMake(0, 0, 240, 40))
        label.text = "登录后，最新、最热微博尽在掌握，不再会与实事潮流擦肩而过"
        label.textColor = UIColor.redColor()
        label.font = UIFont.systemFontOfSize(14)
        label.textAlignment = NSTextAlignment.Center
        label.preferredMaxLayoutWidth = 224
        // 最多两行，超过两行之后 用。。。表示
        label.numberOfLines = 2
        label.sizeToFit()
        
        return label
    }()
    
    // 注册按钮 
    
    lazy var registerButton:UIButton = {
       
        let btn = UIButton(frame: CGRectMake(0, 0,100, 40))
        
        btn.setTitle("注册", forState: UIControlState.Normal)
        btn.setTitleColor(UIColor.darkGrayColor(), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        //添加方法
        btn.addTarget(self, action: "register", forControlEvents: UIControlEvents.TouchUpInside)
        return btn
    }()
    
    // 登录按钮
    
    lazy var loginButton:UIButton = {
        let btn = UIButton(frame: CGRectMake(200, 220, 100, 40))
        btn.setTitle("登录", forState: UIControlState.Normal)
        btn.setTitleColor(UIColor.darkGrayColor(), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        // 添加方法
        btn.addTarget(self, action: "login", forControlEvents: UIControlEvents.TouchUpInside)
        return btn
    }()

}
